package com.ljd.music.controller;


import com.alibaba.fastjson.JSONObject;
import com.ljd.music.dao.SongListMapper;
import com.ljd.music.pojo.SongList;
import com.ljd.music.service.SongListService;
import com.ljd.music.util.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("songList")
public class SongListController {

    @Autowired
    private SongListService songListService;

    @PostMapping("add")
    public Object addSongList(SongList  songList) {
        JSONObject jsonObject = new JSONObject();
        songList.setPic("/images/songListPic/歌单默认图标.jpg");


        boolean insert = songListService.insert(songList);

        if(insert) {
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"添加成功");
        }else {
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"添加失败");
        }
        return jsonObject;
    }

    @PostMapping("update")
    public Object updateSongList(SongList songList) {
        JSONObject jsonObject = new JSONObject();

        boolean update = songListService.update(songList);


        if(update) {
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"修改成功");
        }else {
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"修改失败");
        }
        return jsonObject;

    }

    @GetMapping("allSongList")
    public Object allSongList () {
        return songListService.allSongList();
    }

    @GetMapping("delete")
    public Object deleteSongList (Integer id) {
        JSONObject jsonObject = new JSONObject();
        boolean delete = songListService.delete(id);

        if(delete) {
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"删除成功");
        }else {
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"删除失败");
        }
        return jsonObject;
    }
}
